import { createSlice } from "@reduxjs/toolkit";
import { useSelector } from "react-redux";
import { getHospitalSet, getHospitalSetLockId } from "@/api/hosp";
import { log } from "console";

const hospSlice = createSlice({
    name: "hosp",
    initialState: {
        hospSetList: []
    },
    reducers: {
        upHospSetList(state, { payload }) {

            state.hospSetList = payload;
        },

    }
})
const { upHospSetList } = hospSlice.actions;

//获取状态列表
export const getHospitalSetAsync = (page: number, limit: number) => {
    return async (dispatch: any) => {
        const result = await getHospitalSet(page, limit);
        dispatch(upHospSetList(result.data.records))
    }
}



// 完成锁定2:更新服务端的锁定状态，然后重新获取当前页的信息，进行渲染
// 优点：可以将最后操作以后的最新数据进行呈现
export const getHospitalSetLockIdAsync = (id: number, status: number) => {
    return async (dispatch: any) => {
        await getHospitalSetLockId(id, status);
        dispatch(getHospitalSetAsync(1, 5));
    }
}



// 导出状态
export const useSelectorHosp = () => useSelector((state: any) => state.hosp);
export default hospSlice.reducer;